package com.neusoft.shopingapi.controller;

import com.neusoft.shopingapi.entity.CartEntity;
import com.neusoft.shopingapi.entity.GoodsEntity;
import com.neusoft.shopingapi.service.CartService;
import com.neusoft.shopingapi.service.GoodsService;
import com.neusoft.shopingapi.service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@CrossOrigin
@RestController
public class Cartcontroller {
    @Autowired
    private CartService cartService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private PictureService pictureService;

    @GetMapping("/qureyCart")
    public Map<String,Object> qureyCart(Integer userno){
        List<CartEntity> cartList = cartService.queryCart(userno);
        List<Map> list=new ArrayList<>();
        cartList.forEach(cartEntity -> {
           GoodsEntity goodsEntity= goodsService.getGoodsByOne(cartEntity.getGoodsno());
            List<String> images=pictureService.qureyFirstUrl(goodsEntity.getGoodsno());
            goodsEntity.setImages(images);
            Map<String,Object> map = new ConcurrentHashMap<>();
            map.put("goods",goodsEntity);
            map.put("cart",cartEntity);
            list.add(map) ;
        });
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","查询成功!!!");
        map.put("data",list);
        return map;
    }

    @RequestMapping("/addCart")
    public Map<String,Object> addCart(@RequestBody CartEntity cartEntity){

       Integer flag= cartService.addCart(cartEntity);
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","添加成功!!!");
        map.put("flag",flag);
        return map;
    }

    @RequestMapping("/updateCart")
    public Map<String,Object> updateCart(@RequestBody CartEntity cartEntity){
        Integer flag=cartService.updateCart(cartEntity);
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","修改成功!!!");
        map.put("flag",flag);
        return map;
    }

    @RequestMapping("/delCart")
    public Map<String,Object> delCart(@RequestBody CartEntity cartEntity){
        Integer flag=cartService.delCart(cartEntity.getCartno());
        Map<String,Object> map = new ConcurrentHashMap<>();
        map.put("code",new Integer(100000));
        map.put("msg","删除成功!!!");
        map.put("flag",flag);
        return map;
    }
}
